package org.jeecg.modules.geo.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.core.constant.DataStatusEnum;
import org.jeecg.modules.core.constant.YesOrNotEnum;
import org.jeecg.modules.geo.entity.BimGeoApp;
import org.jeecg.modules.geo.mapper.BimGeoAppMapper;
import org.jeecg.modules.geo.model.BimGeoAppAuthConfig;
import org.jeecg.modules.geo.service.IBimGeoAppService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: geo应用管理
 * @Author: jeecg-boot
 * @Date: 2022-11-29
 * @Version: V1.0
 */
@Service
@Slf4j
public class BimGeoAppServiceImpl extends ServiceImpl<BimGeoAppMapper, BimGeoApp> implements IBimGeoAppService {

    @Override
    public List<Integer> allIdNotDelete() {
        LambdaUpdateWrapper<BimGeoApp> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(BimGeoApp::getDelFlag, DataStatusEnum.NORMAL.getCode());
        List<BimGeoApp> bimGeoApps = this.getBaseMapper().selectList(queryWrapper);
        return CollectionUtils.isEmpty(bimGeoApps) ? null : bimGeoApps.stream().map(BimGeoApp::getId).collect(Collectors.toList());
    }

    @Override
    public void add(BimGeoApp bimGeoApp) {
        this.verifyAppInfo(bimGeoApp, null);
        bimGeoApp.setDelFlag(DataStatusEnum.NORMAL.getCode());
        this.save(bimGeoApp);
    }

    @Override
    public void update(BimGeoApp bimGeoApp) {
        if (bimGeoApp.getId() == null) {
            log.error("修改应用失败，id不能为空");
            throw new JeecgBootException("修改应用失败，id不能为空");
        }
        BimGeoApp geoApp = this.getById(bimGeoApp.getId());
        this.verifyAppInfo(bimGeoApp, geoApp);
        this.updateById(bimGeoApp);
    }


    public BimGeoApp find(SFunction<BimGeoApp, ?> column, Object val) {
        LambdaQueryWrapper<BimGeoApp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(column, val);
        //启用
        queryWrapper.eq(BimGeoApp::getEnableFlag, YesOrNotEnum.Y.getCode());
        List<BimGeoApp> bimGeoApps = this.getBaseMapper().selectList(queryWrapper);
        return CollectionUtils.isEmpty(bimGeoApps) ? null : bimGeoApps.get(0);
    }

    @Override
    public List<BimGeoApp> findList(SFunction<BimGeoApp, ?> column, Object val) {
        LambdaQueryWrapper<BimGeoApp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(column, val)
                .eq(BimGeoApp::getDelFlag, DataStatusEnum.NORMAL.getCode());
        return this.getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public BimGeoAppAuthConfig getAuthConfig(Integer appId) {
        BimGeoApp bimGeoApp = this.getById(appId);
        return this.parseAuthConfig(bimGeoApp.getAuthConfig());
    }

    @Override
    public void editAuthConfig(BimGeoAppAuthConfig bimGeoAppAuthConfig) {
        BimGeoApp bimGeoApp = this.getById(bimGeoAppAuthConfig.getAppId());
        if (bimGeoApp == null) {
            throw new JeecgBootException("修改授权配置失败，应用不存在，appId=" + bimGeoAppAuthConfig.getAppId());
        }
        if (!StringUtils.isEmpty(bimGeoAppAuthConfig.getAuthErrorCodes())) {
            bimGeoAppAuthConfig.setAuthErrorCodes(bimGeoAppAuthConfig.getAuthErrorCodes().replaceAll("，", ","));
        }
        bimGeoApp.setAuthConfig(JSONUtil.toJsonStr(bimGeoAppAuthConfig));
        this.update(bimGeoApp);
    }

    private void verifyAppInfo(BimGeoApp newBimGeoApp, BimGeoApp oldBimGeoApp) {
        BimGeoApp queryByCodeParam = this.find(BimGeoApp::getAppCode, newBimGeoApp.getAppCode());
        if (queryByCodeParam != null) {
            if (oldBimGeoApp != null && queryByCodeParam.getId().equals(oldBimGeoApp.getId())) {
                return;
            }
            throw new JeecgBootException("添加失败，编码已存在！！！");
        }
        BimGeoApp queryByNameParam = this.find(BimGeoApp::getAppName, newBimGeoApp.getAppName());
        if (queryByNameParam != null) {
            if (oldBimGeoApp != null && queryByNameParam.getId().equals(oldBimGeoApp.getId())) {
                return;
            }
            throw new JeecgBootException("添加失败，应用名称已存在！！！");
        }
    }

    private BimGeoAppAuthConfig parseAuthConfig(String authConfigStr) {
        // 解析授权信息
        return org.apache.commons.lang3.StringUtils.isBlank(authConfigStr) ? null : JSONUtil.toBean(authConfigStr, BimGeoAppAuthConfig.class);
    }
}
